package com.campus.lostandfound.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.campus.lostandfound.common.Result;
import com.campus.lostandfound.entity.ClaimRequest;
import com.campus.lostandfound.service.ClaimRequestService;
import com.campus.lostandfound.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 认领申请Controller
 */
@RestController
@RequestMapping("/claim")
@CrossOrigin
public class ClaimRequestController {
    
    @Autowired
    private ClaimRequestService claimRequestService;
    
    @Autowired
    private JwtUtil jwtUtil;
    
    /**
     * 提交认领申请
     */
    @PostMapping("/submit")
    public Result<String> submitClaim(@RequestHeader("Authorization") String token,
                                      @RequestBody ClaimRequest claimRequest) {
        try {
            String userId = jwtUtil.getUserIdFromToken(token);
            claimRequest.setApplicantId(userId);
            
            claimRequestService.submitClaim(claimRequest);
            return Result.success("提交成功，等待审核");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取我的认领申请
     */
    @GetMapping("/my")
    public Result<IPage<ClaimRequest>> getMyClaims(@RequestHeader("Authorization") String token,
                                                   @RequestParam(defaultValue = "1") Integer current,
                                                   @RequestParam(defaultValue = "10") Integer size) {
        try {
            String userId = jwtUtil.getUserIdFromToken(token);
            IPage<ClaimRequest> page = claimRequestService.getMyClaims(userId, current, size);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取待审核的认领申请（管理员）
     */
    @GetMapping("/pending")
    public Result<IPage<ClaimRequest>> getPendingClaims(@RequestParam(defaultValue = "1") Integer current,
                                                        @RequestParam(defaultValue = "10") Integer size) {
        try {
            IPage<ClaimRequest> page = claimRequestService.getPendingClaims(current, size);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 审核认领申请（管理员）
     */
    @PostMapping("/audit")
    public Result<String> auditClaim(@RequestBody Map<String, Object> params) {
        try {
            Long id = Long.valueOf(params.get("id").toString());
            Integer status = (Integer) params.get("status");
            String auditComment = (String) params.get("auditComment");
            
            claimRequestService.auditClaim(id, status, auditComment);
            return Result.success("审核成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}

